import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiandou/Common.dart';
import 'package:xiandou/Request/DioManager.dart';
import 'package:xiandou/Request/NWApi.dart';
import 'package:xiandou/Request/NWMethod.dart';
import 'package:xiandou/personalcenter/WithdrawalRecordController.dart';

class WithdrawalControllerScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() =>  WithdrawalControllerState();
}

class WithdrawalControllerState extends State<WithdrawalControllerScreen> {

  bool isAliPay =true;
  String balance ='0';
  var amountController = TextEditingController();
  var accountController = TextEditingController();
  var nameController = TextEditingController();

  void _submitWithDrawalAction() {
    if(amountController.text.length<1) {
      Fluttertoast.showToast(msg: '请输入提现金额');
      return;
    }
    if(accountController.text.length<1) {
      Fluttertoast.showToast(msg: '请输入提现账号');
      return;
    }
    if(nameController.text.length<1) {
      Fluttertoast.showToast(msg: '请输入姓名');
      return;
    }
    var params ={'applyMoney':amountController.text,'realName':nameController.text,'paymentAccount':accountController.text,'paymentType':isAliPay ? '2' :'1'};
    DioManager().request(NWMethod.POST, NWApi.coinApplyWithdrawalApi,params: params,success: (data){
      Fluttertoast.showToast(msg: '申请提现成功');
      Navigator.pop(context);
    },error: (error){
      Fluttertoast.showToast(msg: error.message);
    });
  }

  void getCurrentUserInfo (){
    DioManager().request(NWMethod.GET, NWApi.getUserInfoApi,params: null,success: (data){
      setState(() {
        balance = data['balance'].toString();
      });
    },error: (error){
      Fluttertoast.showToast(msg: error.message);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    getCurrentUserInfo();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CupertinoNavigationBar(
          backgroundColor: Colors.white,
          middle: const Text('提现'),
          trailing: FlatButton(
            child: Text('提现记录'),
            onPressed: (){
              Navigator.push(
                context,
                new MaterialPageRoute(builder: (context) => new WithdrawalRecordController()),
              );
            },
          ),
        ),
        body: Container(
          child: ListView(
            children: <Widget>[
              Container(
//                height: 134,
                width: MediaQuery.of(context).size.width-20,
                margin: const EdgeInsets.fromLTRB(10, 9, 10, 5),
                decoration: new BoxDecoration(
                    color: Color(0xffFF5236),
                    borderRadius: BorderRadius.all(Radius.circular(5))
                ),
                child: Column(
                  children: <Widget>[
                    Padding(
                      child:Text('账户总额（闲豆）',style: TextStyle(color: Color(0xffFFFFFF)),),
                      padding: const EdgeInsets.only(top: 25,bottom: 5),
                    ),
                    Text(balance,style: TextStyle(color: Color(0xffFFFFFF),fontWeight: FontWeight.bold,fontSize: 25),),
                    Padding(
                      padding: const EdgeInsets.only(left: 31,bottom: 15,top: 13),
                      child: Row(
                        children: <Widget>[
                          Expanded(
                            child: Text('最低提现1000闲豆',style: TextStyle(color: Colors.white),),
                          ),
                          FlatButton(child: Text('规则?',style: TextStyle(color: Colors.white),),),
                        ],
                      )
                    ),
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.only(left: 10,right: 10,top: 12.5),
                decoration: new BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(5))
                ),
                child: Padding(
                  padding: const EdgeInsets.only(left: 22,top: 26),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text('提现金额',style: TextStyle(fontSize:15 ),),
                      SizedBox(height: 24,),
                      Row(
                        children: <Widget>[
                          Text('¥',style: TextStyle(fontSize: 20),),
                          SizedBox(width: 5,),
                          Expanded(
                            child: TextField(
                              keyboardType: TextInputType.number,
                              controller: amountController,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                              ),
                            ),
                          )
                        ],
                      ),
                      Padding(
                        padding: const EdgeInsets.only(bottom: 33,right: 20),
                        child: Divider(height: 1,color: Color(0xffE0E0E0),)
                      )
                    ],
                  ),
                )
              ),
              Container(
                  margin: const EdgeInsets.only(left: 10,right: 10,top: 12.5),
                  decoration: new BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(5))
                  ),
                  child: Padding(
                    padding: const EdgeInsets.only(left: 22,top: 26),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text('提现方式',style: TextStyle(fontSize:15 ),),
                        SizedBox(height: 24,),
                        Row(
                          children: <Widget>[
                            Image(image: AssetImage('assets/d_ali.png'),width: 28,height: 28,),
                            SizedBox(width: 11,),
                            Expanded(
                              child: Text('提现到支付宝'),
                            ),
                            IconButton(
                              icon: Image(image: AssetImage(isAliPay ? 'assets/d_check.png' : 'assets/d_uncheck.png'),),
                              onPressed: () {
                                setState(() {
                                  isAliPay =!isAliPay;
                                });
                              },
                            ),
                            SizedBox(width: 10,),
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Image(image: AssetImage('assets/d_wechat_pay.png'),width: 28,height: 28,),
                            SizedBox(width: 11,),
                            Expanded(
                              child: Text('提现到微信'),
                            ),
                            IconButton(
                              icon: Image(image: AssetImage(isAliPay ? 'assets/d_uncheck.png' : 'assets/d_check.png'),),
                              onPressed: () {
                                setState(() {
                                  isAliPay =!isAliPay;
                                });
                              },
                            ),
                            SizedBox(width: 10,),
                          ],
                        ),
                        SizedBox(height: 15,),
                        TextField(
                          controller: accountController,
                          decoration: InputDecoration(
                            hintText: isAliPay ? '请输入支付宝账号' : '请输入微信账号',
                            border: InputBorder.none,
                          ),
                        ),
                        Container(
                            height: 10,
                            padding: const EdgeInsets.only(bottom: 33,right: 20),
                            child: Divider(height: 1,color: Color(0xffE0E0E0),)
                        ),
                        TextField(
                          controller: nameController,
                          decoration: new InputDecoration(
                            hintText: '请输入真实姓名',
                            border: InputBorder.none,
                          ),
                        ),
                        Container(
                            height: 1,
                            padding: const EdgeInsets.only(bottom: 33,right: 20),
                            child: Divider(height: 1,color: Color(0xffE0E0E0),)
                        ),
//                        SizedBox(height: 1,),
                      ],
                    ),
                  )
              ),
              GestureDetector(
                onTap: _submitWithDrawalAction,
                child:Container(
                  margin: const EdgeInsets.only(top: 50,left: 55,right: 55,bottom: 50),
                  height: 50,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(//背景装饰
                      gradient: RadialGradient( //背景径向渐变
                          colors: [Color(0xffEE5F20), Color(0xffED4327)],
                          center: Alignment.topLeft,
                          radius: 5
                      ),
                      borderRadius: new BorderRadius.circular((20.0)),
                      boxShadow: [ //卡片阴影
                        BoxShadow(
                            color: Color(0xffEE5F20),
                            offset: Offset(2.0, 2.0),
                            blurRadius: 4.0
                        )
                      ]
                  ),
                  child: Text('申请提现', style: TextStyle(color: Colors.white, fontSize: 17),),
                ),
              )
            ],
          ),
        )
    );
  }
}